package com.cyxu.moviedigest.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.cyxu.moviedigest.R;
import com.cyxu.moviedigest.activities.MovieDetailActivity;
import com.cyxu.moviedigest.data.AppConstant;
import com.cyxu.moviedigest.data.MovieInfo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MovieInfo> movies = new ArrayList<>();
    private Context context;
    public final int TYPE_HEADER = 0;
    public final int TYPE_ITEM = 1;


    public MyAdapter(Context context, List<MovieInfo> movieSet)  {
        super();
        this.movies = movieSet;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        if (viewType == TYPE_HEADER) {
            View hView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.header_item,viewGroup,false);;
            return new ViewHolder(hView, TYPE_HEADER);
        }
        else {
            View iView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.movie_list_item,viewGroup,false);
            return new ViewHolder(iView, TYPE_ITEM);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        if (getItemViewType(position) == TYPE_HEADER) {
            viewHolder.dateView.setText(new SimpleDateFormat("MMMM d", Locale.ENGLISH).format(new Date()));
            viewHolder.weekView.setText(new SimpleDateFormat("EEEE", Locale.ENGLISH).format(new Date()));

            final ViewHolder vh = viewHolder;
            String url = movies.get(0).getImage();
            RequestQueue queue = Volley.newRequestQueue(context);
            ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    vh.headerView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    vh.headerView.setImageBitmap(bitmap);
                }
            }, 0, 0, ImageView.ScaleType.CENTER_CROP, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            queue.add(imageRequest);

        }
        else {
            viewHolder.movieTitleView.setText(movies.get(position - 1).getTitle());
            viewHolder.movieDescView.setText(movies.get(position - 1).getDescription());
        }
    }

    @Override
    public int getItemCount() {
        return movies.size() + 1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView dateView;
        public TextView weekView;
        public ImageView headerView;
        public TextView movieTitleView;
        public TextView movieDescView;

        public ViewHolder(View view, int viewType){
            super(view);
            if (viewType == TYPE_HEADER) {
                dateView = (TextView) view.findViewById(R.id.dateText);
                weekView = (TextView) view.findViewById(R.id.dateText2);
                headerView = (ImageView) view.findViewById(R.id.HeadMoviePic);
            }
            else {
                movieTitleView = (TextView) view.findViewById(R.id.movie_title);
                movieDescView = (TextView) view.findViewById(R.id.movie_info);
                view.setOnClickListener(this);
            }
        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, MovieDetailActivity.class);
            intent.putExtra(AppConstant.EXTRA_MOVIE, (MovieInfo)movies.get(getPosition() - 1));
            Log.i("MyAdapter_movie1", Integer.toString(getPosition()));
            Log.i("MyAdapter_movie1", ((MovieInfo)movies.get(getPosition() - 1)).getTitle());
            context.startActivity(intent);
        }

    }
}